//
//  XCStatus.swift
//  小陈微博Swift用纯代码创建
//
//  Created by 小陈 on 16/3/2.
//  Copyright © 2016年 小陈. All rights reserved.
//


//微博的数据模型 (包含 用户的数据模型)
import UIKit

class XCStatus: NSObject {
    // 微博创建时间
    var created_at: String?
    
    // 微博ID
    var id:	Int = 0
    
    // 微博信息内容
    var text: String?
    
    // 微博来源
    var source: String?
    
    /// 定义一个存储图片URL的数组
    var storePictureURLs: [NSURL]?
    
    // 微博配图: 数组里面放的是字典
    var pic_urls: [[String: AnyObject]]?{
        didSet{
            storePictureURLs = [NSURL]()
            let count = pic_urls?.count ?? 0
            
            if count == 0{
                return
            }
            for dict in pic_urls!{
                let urlString = dict["thumbnail_pic"] as! String
                let url = NSURL(string: urlString)!
                storePictureURLs?.append(url)
//            print("storePictureURLs \(storePictureURLs)")
            }
            
            
        }
    }
    
    // 转发数
    var reposts_count: Int = 0
    
    // 评论数
    var comments_count: Int = 0
    
    // 表态数
    var attitudes_count: Int = 0
    
    // 用户模型
        var user: XCUser?
    
    
    /// 转发的微博数据
    var retweeted_status: XCStatus?
    
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        // 判断key== user 时将value 转换成 user模型
                if  key == "user" {
                    if let dict = value as? [String : AnyObject]{
        
                        user = XCUser(dict: dict)
                    }
        // 要return,不要走父类的将字典赋值给user属性
                    return
                }else if key == "retweeted_status"{
                    if let dict = value as? [String : AnyObject]{
                        retweeted_status = XCStatus(dict: dict)
                    }
        // 要return,不要走父类的将字典赋值给retweeted_status属性
                    return
                    
        }
 
        
        super.setValue(value, forKey: key)
        
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    //
    
    // 打印
    override var description: String{
        let Key = ["created_at","id","text","source","pic_urls","reposts_count","comments_count","attitudes_count"]
        return dictionaryWithValuesForKeys(Key).description
    }
    
    
    
}
